/* Distort a text by forming 3 groups each of which innerly distorted from the original text's order,
 * outerly differentiated from the other 2 groups based on the contained characters, namely
 * CJK (Chinese, Japanese, Korean), Phonetic, and common text. Because groups 2 and 3 both are based on
 * occidental characters, the parameters/criteria to take each into it's group is a ratio of
 * the number of non-ASCII Latin chars over the strict ASCII of them per line.
 */

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class DistortCJKFonetics {
	public static List chino=new ArrayList(), fonetica=new ArrayList(), traduccion=new ArrayList();
	
	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Gabo/Big small a lot.txt"), "UTF-8"));
		String linea;
		while((linea=reader.readLine())!=null) {
			if(linea.isEmpty())
				continue;
			switch(tipoLinea(linea)) {
				case CHINO:
					registrarLineaChino(linea);
					break;
				case FONETICA:
					registrarLineaFonetica(linea);
					break;
				case TRADUCCION:
					registrarLineaTraduccion(linea);
					break;
			}
		}
		reader.close();
		desordenar(chino);
		desordenar(fonetica);
		desordenar(traduccion);
		
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Gabo/Big small a lot desordenado 2.txt"), "UTF-8"));
		grabar(writer, chino);
		writer.newLine(); 		writer.newLine(); 		writer.newLine();
		grabar(writer, fonetica);
		writer.newLine(); 		writer.newLine(); 		writer.newLine();
		grabar(writer, traduccion);
		writer.close();
	}
	
	public static TipoLinea tipoLinea(String linea) {
		int chino=0, fonetica=0, traduccion=0;
		for(int i=0; i<linea.length(); i++) {
			System.out.println(Character.UnicodeBlock.of(linea.charAt(i)).toString());
			if(Character.UnicodeBlock.of(linea.charAt(i)).toString().matches(".*CJK.*"))
				chino++;
			else if(Character.UnicodeBlock.of(linea.charAt(i)).toString().equals("BASIC_LATIN"))
				traduccion++;
			else if(Character.UnicodeBlock.of(linea.charAt(i)).toString().matches(".*LATIN.*"))
				fonetica++;
			
		}
		if(chino!=0)
			return TipoLinea.CHINO;
		if(fonetica!=0 && traduccion/fonetica<=9)
			return TipoLinea.FONETICA;
		return TipoLinea.TRADUCCION;
		
	}
	
	public static Pattern pChino = Pattern.compile(".");
	public static void registrarLineaChino(String linea) {
		Matcher m = pChino.matcher(linea);
		while(m.find())
			chino.add(m.group());
	}
	
	public static Pattern pFonetica = Pattern.compile("[^\\s]+");
	public static void registrarLineaFonetica(String linea) {
		Matcher m = pFonetica.matcher(linea);
		while(m.find())
			fonetica.add(m.group());
	}
	
	public static Pattern pTraduccion = Pattern.compile("[^\\s]+");
	public static void registrarLineaTraduccion(String linea) {
		Matcher m = pTraduccion.matcher(linea);
		while(m.find())
			traduccion.add(m.group());
	}
	
	public static void desordenar(List elementos) {
		for(int a=0; a<elementos.size(); a++) {
			int b = (int)(Math.random() * elementos.size());
			
			Object aux = elementos.get(a);
			elementos.set(a, elementos.get(b));
			elementos.set(b, aux);
		}
	}
	
	public static void grabar(BufferedWriter writer, Collection elementos) throws Exception {
		for(Iterator lnIt = elementos.iterator(); lnIt.hasNext();) {
			String unidad = (String)lnIt.next();
			writer.write(unidad, 0, unidad.length());
			writer.write("   ", 0, 3);
		}
	}
	
	public static enum TipoLinea {
		CHINO,FONETICA,TRADUCCION;
	}

}